package com.zscat.mallplus.center.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.center.BaseController;
import com.zscat.mallplus.center.entity.CenterCash;
import com.zscat.mallplus.center.service.ICenterCashService;
import com.zscat.mallplus.fenxiao.entity.FenxiaoMemberTotal;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IFenxiaoMemberTotalService;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/center/cash")
public class CenterCashController extends BaseController{

	@Resource
	private ICenterCashService centerCashService;
	@Resource
	private IUmsMemberService memberService;
	@Resource
	private IFenxiaoMemberTotalService fenxiaoMemberTotalService;
	
	@GetMapping("list")
	public Object getSysStoreCashByPage(CenterCash entity,
			@RequestParam(value = "kw", required = false) String kw,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
			) {
		try {
			entity.setUserId(memberService.getNewCurrentMember().getId());
			QueryWrapper<CenterCash> wrapper = new QueryWrapper<>(entity).orderByDesc("create_time");
			if (kw != null) {
				kw = kw.replace(" ", "");
				kw = "%" + kw + "%";
				wrapper.like("phone", kw);
			}

			return new CommonResult().success(centerCashService.page(new Page<CenterCash>(pageNum, pageSize), wrapper));
		} catch (Exception e) {
			log.error("根据条件查询所有提现列表：%s", e.getMessage(), e);
		}
		return new CommonResult().failed();
	}
	
    @PostMapping("/add")
    public Object add(CenterCash entity)
    {
    	UmsMember member = memberService.getNewCurrentMember();
    	entity.setUserId(member.getId());
    	entity.setNickName(member.getNickname());
    	entity.setPhone(member.getValidatePhone());
    	entity.setCreateTime(new Date());
    	FenxiaoMemberTotal fenxiaoMemberTotal = fenxiaoMemberTotalService.getOne(new QueryWrapper<FenxiaoMemberTotal>().eq("id", member.getId()));
    	if(fenxiaoMemberTotal.getCurrentAmout().compareTo(entity.getMoney()) == -1 ) {
    		return new CommonResult().failed("余额不足");
    	}
    	fenxiaoMemberTotal.setCurrentAmout(fenxiaoMemberTotal.getCurrentAmout().subtract(entity.getMoney()));
    	fenxiaoMemberTotalService.updateById(fenxiaoMemberTotal);
        return toAjax(centerCashService.save(entity));
    }
}
